package io.github.cubelitblade.librarymanagementbackend.controller;

import io.github.cubelitblade.librarymanagementbackend.dto.UserBookRequestDTO;
import io.github.cubelitblade.librarymanagementbackend.service.UserBorrowFacade;
import io.github.cubelitblade.librarymanagementbackend.utils.ResultContext;
import io.github.cubelitblade.librarymanagementbackend.vo.BorrowResponseVO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/borrows")
public class BorrowController {
    //构造器注入
    private final UserBorrowFacade userBorrowFacade;

    public BorrowController(UserBorrowFacade userBorrowFacade) {
        this.userBorrowFacade = userBorrowFacade;
    }

    @PostMapping
    public ResponseEntity<ResultContext<BorrowResponseVO>> borrowBook(@RequestBody UserBookRequestDTO userBookRequestDTO) {
        return ResponseEntity.ok(ResultContext.succeed(userBorrowFacade.borrowBook(userBookRequestDTO)));
    }

    @PatchMapping("/{borrowId}/return")
    public ResponseEntity<ResultContext<BorrowResponseVO>> returnBook(@PathVariable Integer borrowId) {
        return ResponseEntity.ok(ResultContext.succeed(userBorrowFacade.returnBook(borrowId)));
    }

    @GetMapping
    public ResponseEntity<ResultContext<BorrowResponseVO>> findActiveBorrowRecord(@RequestParam Integer userId, @RequestParam Integer bookId) {
        return ResponseEntity.ok(ResultContext.succeed(userBorrowFacade.findActiveBorrowRecord(userId, bookId)));
    }
}